import { request } from "../../utils/request"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: 50,
    orderList: [], //订单列表数据
    currentIndex: "0", //当前tab标签索引
    page: 1, //当前页
    pageSize: 8, //每页记录数
    totalPage: 0, //页数
    tabs: [
      {
        index: "0",
        name: "全部"
      },
      {
        index: "1",
        name: "待付款",
      },
      {
        index: "2",
        name: "待发货"
      },
      {
        index: "3",
        name: "待收货"
      },
      {
        index: "4",
        name: "已完成"
      }
    ]
  },

  /**
   * 取消订单
   */
  cancelOrder() {
    wx.showActionSheet({
      itemList: ['不想买了', '买错了', '地址有误'],
      alertText: "删除删除",
      success(res) {
        console.log(res.tapIndex)
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },

  /**
   * 加载订单数据
   */
  async loadOrderData() {
    //如果数据已经是最后一页了，则不再加载数据
    if (this.data.totalPage !== 0 && this.data.totalPage - this.data.page < 0) {
      return
    }
    const { token } = wx.getStorageSync("user")
    //构建请求参数
    const param = {
      page: this.data.page,
      pageSize: this.data.pageSize,
      status: this.data.currentIndex == "0" ? "" : this.data.currentIndex
    }
    const res = await request({ url: "/order/listOrder", method: "GET", data: param, header: { "Authorization": token } })
    if (res.data.state === 0) {
      const dataList = res.data.data.list
      //如果有数据，追加到之前的数组中
      if (dataList && dataList.length > 0) {
        const orderList = this.data.orderList.concat(dataList)
        this.setData({
          orderList,
          page: this.data.page + 1,
          totalPage: res.data.data.totalPage
        })
      }
    }
    this.calcSwiperHeight()
  },

  /**
   * 计算swiper高度
   */
  calcSwiperHeight() {
    //tabs的高度
    let tabHeight = 0
    //swiper的高度
    let swiperHeight = 0
    //获取tabs的高度
    wx.createSelectorQuery().select(".tab").boundingClientRect(res => {
      tabHeight = res.height
    }).exec()
    //计算swiper的高度
    wx.createSelectorQuery().selectAll(".order-info").boundingClientRect(res => {
      res.forEach((v, i) => {
        swiperHeight += v.height
      })
      //获取当前手机的高度
      const screenHeight = wx.getSystemInfoSync().windowHeight
      //如果当前的swiper高度大于手机屏幕的高度-tab的高度，则采用swiper的高度，否则采用手机屏幕的高度-tab的高度
      swiperHeight = swiperHeight >= (screenHeight - tabHeight) ? swiperHeight + 12 * res.length : screenHeight - tabHeight
      this.setData({
        height: swiperHeight,
      })
    }).exec()
  },

  /**
   * 切换tab页
   */
  tabChoose(event) {
    //如果和之前的tab页相同，不做操作
    if (this.data.currentIndex == event.currentTarget.dataset.index) {
      return
    }
    this.setTabs(event.currentTarget.dataset.index)
    this.loadOrderData()
  },

  /**
   * 切换swiper
   */
  swiperChange(event) {
    //如果和之前的tab页相同，不做操作
    if (this.data.currentIndex == event.detail.current) {
      return
    }
    this.setTabs(event.detail.current)
    this.loadOrderData()
  },

  /**
   * 设置tab页
   */
  setTabs(index) {
    //设置所选tab页，并重置分页数据
    this.setData({
      currentIndex: index,
      orderList: [],
      page: 1,
      pageSize: 8,
      totalPage: 0
    })
    //页面滚动到头部位置
    wx.pageScrollTo({
      selector: ".main",
      duration: 300
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //获取选择的订单tab
    const currentIndex = options.currentIndex
    if (currentIndex) {
      this.setData({
        currentIndex: options.currentIndex
      })
    }
    //加载订单数据
    this.loadOrderData()
  },

  /**
  * 页面相关事件处理函数--监听用户上拉动作
  */
  onReachBottom() {
    //分页查询
    this.loadOrderData()
  }
})